import Vue from "vue";
import VueRouter from "vue-router";

const error = () => import("@/views/404.vue"); // 错误页面
const Login = () => import("@/views/login"); // 登录页面
const EmailLogin = () => import("@/views/login/emailLogin"); // 邮箱登录
const RegisterAccount = () => import("@/views/login/registerAccount"); // 注册账号
const ForgetPassword = () => import("@/views/login/forgetPassword"); // 忘记密码
const HomeIndex = () => import("@/views"); // 路由通道
const Transfer = () => import("@/views/transfer.vue"); //中转路由
const HomePage = () => import("@/views/homePage"); // 首页
const Announcement = () => import("@/views/homePage/announcement"); // 公告
const AnnouncementList = () => import("@/views/homePage/announcement/announcementList"); // 公告列表
const AnnouncementListDetail = () => import("@/views/homePage/announcement/announcementListDetail"); // 公告列表详情
const AiDrawing = () => import("@/views/homePage/aiDrawing"); // 首页ai绘画
const AiDrawingHistory = () => import("@/views/homePage/aiDrawingHistory"); // 首页ai绘画历史
const AiCreationDetail = () => import("@/views/homePage/aiCreationDetail") // 首页ai创作点击详情页
const AiChat = () => import("@/views/homePage/aiChat"); // 首页ai聊天
const AiQuantization = () => import("@/views/aiQuantization"); // ai量化
const MyOrder = () => import("@/views/aiQuantization/myOrder"); // 我的订单
const OrderDetail = () => import("@/views/aiQuantization/orderDetail"); // 订单详情
const ChatTools = () => import("@/views/chatTools"); // 聊天
const PersonalCenter = () => import("@/views/personalCenter"); // 个人中心
const PersonalUserSetting = () => import("@/views/personalCenter/personalUserSetting"); // 个人设置
const PersonalSettingCompile = () => import("@/views/personalCenter/personalUserSetting/personalSettingCompile"); // 个人设置编辑页
const PersonalSettingLevel = () => import("@/views/personalCenter/personalUserSetting/personalSettingLevel"); // 个人设置会员等级页
const PersonalSettingRealName = () => import("@/views/personalCenter/personalUserSetting/personalSettingRealName"); // 个人设置实名认证页
const PersonalSettingAuthentication = () => import("@/views/personalCenter/personalUserSetting/personalSettingAuthentication"); // 个人设置实名认证页
const PersonalCenterHistory = () => import("@/views/personalCenter/personalCenterHistory"); // 个人中心历史记录页
const PersonalCenterHistoryDetail = () => import("@/views/personalCenter/personalCenterHistory/historyDetail"); // 个人中心历史记录详情页

const PersonalSetting = () => import("@/views/personalCenter/personalSetting"); // 系统设置
Vue.use(VueRouter);
const NationalIndexBar = () => import("@/views/personalCenter/nationalIndexBar"); // 国家选择索引
const PersonalSettingSwitch = () => import("@/views/personalCenter/personalSetting/personalSettingSwitch"); // 系统设置中转页面
const TopUp = () => import("@/views/personalCenter/topUp"); // 充值
const TopUpDetail = () => import("@/views/personalCenter/topUp/topUpDetail"); // 充值详情
const WithdrawDeposit = () => import("@/views/personalCenter/withdrawDeposit"); // 提现
const FlashExchange = () => import("@/views/personalCenter/flashExchange"); // 闪兑
const CurrencyDetail = () => import("@/views/personalCenter/currencyDetail"); // 币种详情

const routes = [
	{
		path: "/",
		name: "HomeIndex",
		component: HomeIndex,
		redirect: 'homePage',
		children:[
			{
				path: "homePage",
				name: "HomePage",
				component: HomePage,
			},
			{
				path: "aiQuantization",
				name: "AiQuantization",
				component: AiQuantization,
			},
			{
				path: "chatTools",
				name: "ChatTools",
				component: ChatTools,
			},
			{
				path: "personalCenter",
				name: "PersonalCenter",
				component: PersonalCenter,
			}
		]
	},
	{ //中转路由
		path: "/transfer",
		name: "Transfer",
		component: Transfer,
		children: [
			{
				path: "announcement",
				name: "Announcement",
				component: Announcement,
			},
			{
				path: "announcementList",
				name: "AnnouncementList",
				component: AnnouncementList,
			},
			{
				path: "announcementListDetail",
				name: "AnnouncementListDetail",
				component: AnnouncementListDetail,
			},
			{
				path: "aiCreationDetail",
				name: "AiCreationDetail",
				component: AiCreationDetail,
			},
			{
				path: "myOrder",
				name: "MyOrder",
				component: MyOrder,
			},
			{
				path: "orderDetail",
				name: "OrderDetail",
				component: OrderDetail,
			},
			{
				path: "personalSetting",
				name: "PersonalSetting",
				component: PersonalUserSetting,
			},
			{
				path: "personalSettingCompile",
				name: "PersonalSettingCompile",
				component: PersonalSettingCompile,
			},
			{
				path: "personalSettingLevel",
				name: "PersonalSettingLevel",
				component: PersonalSettingLevel,
			},
			{
				path: "personalSettingRealName",
				name: "PersonalSettingRealName",
				component: PersonalSettingRealName,
			},
			{
				path: "personalSettingAuthentication",
				name: "PersonalSettingAuthentication",
				component: PersonalSettingAuthentication,
			},
			{
				path: "personalCenterHistory",
				name: "PersonalCenterHistory",
				component: PersonalCenterHistory,
			},
			{
				path: "personalCenterHistoryDetail",
				name: "PersonalCenterHistoryDetail",
				component: PersonalCenterHistoryDetail,
			},
			{
				path: "systemSettings",
				name: "SystemSettings",
				component: PersonalSetting,
			},
			{
				path: "nationalIndexBar",
				name: "NationalIndexBar",
				component: NationalIndexBar,
			},
			{
				path: "personalSettingSwitch",
				name: "PersonalSettingSwitch",
				component: PersonalSettingSwitch,
			},
			{
				path: "topUp",
				name: "TopUp",
				component: TopUp,
			},
			{
				path: "topUpDetail",
				name: "TopUpDetail",
				component: TopUpDetail,
			},
			{
				path: "withdrawDeposit",
				name: "WithdrawDeposit",
				component: WithdrawDeposit,
			},
			{
				path: "flashExchange",
				name: "FlashExchange",
				component: FlashExchange,
			},
			{
				path: "currencyDetail",
				name: "CurrencyDetail",
				component: CurrencyDetail,
			},
			{
				path: "registerAccount",
				name: "RegisterAccount",
				component: RegisterAccount,
			},
			{
				path: "forgetPassword",
				name: "ForgetPassword",
				component: ForgetPassword,
			},
			{
				path: "aiDrawing",
				name: "AiDrawing",
				component: AiDrawing,
			},
			{
				name: "aiDrawingHistory",
				path: "AiDrawingHistory",
				component: AiDrawingHistory,
			},
			{
				path: "aiChat",
				name: "AiChat",
				component: AiChat,
			}
		]
	},
	{
		path: "/login",
		name: "Login",
		component: Login,
	},
	{
		path: "/emailLogin",
		name: "EmailLogin",
		component: EmailLogin,
	},
	{ // 错误页面
		path: '*',
		component: error
	}
]

const router = new VueRouter({
	// mode: "history",
	mode: "hash",
	base: process.env.BASE_URL,
	routes,
});

//前置守卫(guard)
// 修改后正确代码：
router.beforeEach((to, from, next) => {
    const isAuthenticated = !!localStorage.getItem("token");
    
    // 白名单路径
    const whiteList = ['/login','/emailLogin' ,'/transfer/registerAccount' , '/transfer/forgetPassword'];
    
    // 已登录状态访问登录页时重定向到首页
    if (isAuthenticated && to.path === '/login') {
        return next({ name: 'HomePage' });
    }
    
    // 未登录且不在白名单时跳转登录
    if (!isAuthenticated && !whiteList.includes(to.path)) {
        return next({
            path: '/login',
            // query: { redirect: to.fullPath } // 携带跳转来源路径
        });
    }
    
    next();
});


export default router;